package com.gqk.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gqk.common.result.Result;
import com.gqk.common.util.MD5;
import com.gqk.model.system.SysRole;
import com.gqk.model.system.SysUser;
import com.gqk.service.SysUserService;
import com.gqk.vo.system.SysUserQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {
    @Autowired
    private SysUserService service;
    //用户条件分页查询
    @ApiOperation("用户条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result index(@PathVariable Long page,
                        @PathVariable Long limit,
                        SysUserQueryVo sysUserQueryVo){
        //创建page对象
        Page<SysUser> pageParam = new Page<>(page,limit);
        //封装条件，判断条件值不为空
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        //获取条件值
        String keyword = sysUserQueryVo.getKeyword();
        String createTimeBegin = sysUserQueryVo.getCreateTimeBegin();
        String createTimeEnd = sysUserQueryVo.getCreateTimeEnd();

        //判断条件值不为空
        //like 模糊查询
        if(!StringUtils.isEmpty(keyword)) {
            wrapper.like(SysUser::getUsername,keyword).
                    or().like(SysUser::getName,keyword).
                    or().like(SysUser::getPhone,keyword);

        }
        //ge 大于等于
        if(!StringUtils.isEmpty(createTimeBegin)) {
            wrapper.ge(SysUser::getCreateTime,createTimeBegin);
        }
        //le 小于等于
        if(!StringUtils.isEmpty(createTimeEnd)) {
            wrapper.le(SysUser::getCreateTime,createTimeEnd);
        }
        IPage<SysUser> pageModel = service.page(pageParam, wrapper);
        return Result.ok(pageModel);
    }

//根据id查询用户
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id){
        SysUser user = service.getById(id);
        return Result.ok(user);
    }
//保存用户信息
    @ApiOperation("保存用户信息")
    @PostMapping("/save")
    public Result save(@RequestBody SysUser sysUser){
        String password = sysUser.getPassword();
        password = MD5.encrypt(password);
        service.save(sysUser);
        return  Result.ok();
    }
    //更新用户信息
    @PutMapping("/update")
    @ApiOperation("更新用户信息")
    public Result update(@RequestBody SysUser sysUser){
        service.updateById(sysUser);
        return Result.ok();
    }

    @ApiOperation("删除用户信息")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id){
        service.removeById(id);
        return Result.ok();
    }

    @ApiOperation(value = "更新状态")
    @GetMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        service.updateStatus(id, status);
        return Result.ok();
    }

}
